<?php
/* ******************************
 *	Printer Parts List aJax actions
 * ******************************
 */

include('../../../includes/classes/core.php');
include('../../../includes/classes/category.tree.class.php');
$action		= $_REQUEST['act'];
$error		= '';
$data		= '';
$user_id	= $_SESSION['USERID'];

switch ($action) {
    case 'get_add_page':
		$page		= GetPage();
		$data		= array('page'	=> $page);
		
        break;
	case 'get_list':
		$count	= $_REQUEST['count'];
		$hidden	= $_REQUEST['hidden'];
		$district_id	= $_REQUEST['id'];
				
		$rResult = mysql_query("SELECT	`postal_codes`.`id`,
										`postal_codes`.`name`,
										`postal_codes`.`code`
								FROM	`postal_codes`
								WHERE	`postal_codes`.`district_id` = '$district_id'");
        
		$data = array(
			"aaData"	=> array()
		);
		while ( $aRow = mysql_fetch_array( $rResult ) )
		{
			$row = array();
			for ( $i = 0 ; $i < $count ; $i++ )
			{
				/* General output */
				$row[] = $aRow[$i];
				if($i == ($count - 1)){
					$row[] = '<input type="checkbox" name="check_' . $aRow[$hidden] . '" class="check" value="' . $aRow[$hidden] . '" />';
				}
			}
			$row[1] = '№:  '. $row[1];
			$data['aaData'][] = $row;
		}
		
		break;
    case 'add_post_code':
		$district_id	= $_REQUEST['id'];
		$post_code	= $_REQUEST['pc'];
		SaveIdentity($district_id, $post_code);

        break;
    case 'disable':
    	$postal_codes_id	= $_REQUEST['id'];    	
    	Disable($postal_codes_id);
    	break;    	
    default:
       $error = 'Action is Null';
}

$data['error'] = $error;

echo json_encode($data);


/* ******************************
 *	Printer Parts List Functions
 * ******************************
 */

function Disable($postal_codes_id){

	mysql_query("	UPDATE	`postal_codes`
					SET		`district_id` = ''
					WHERE	`id` = '$postal_codes_id'");

}

function SaveIdentity($district_id, $post_code){
	mysql_query("	UPDATE `postal_codes`
					SET		`district_id`			= '$district_id'
					WHERE	CONCAT(`postal_codes`.`code`, ' - ', `postal_codes`.`name`) = '$post_code' || `postal_codes`.`code` = '$post_code'");
}

function GetPage(){
	$data = '
	<div id="dialog-form">
		    <fieldset>
		    	<legend>ძირითადი ინფორმაცია</legend>		    	
		    	<table class="dialog-form-table">
					<tr>
						<td style="width: 70px;"><label for="barcode_client">ინდექსი :</label></td>
						<td>
							<div class="seoy-row" id="post_code_seoy">
								<input type="text" id="post_code" class="idle seoy-large" onblur="this.className=\'idle seoy-large\'" onfocus="this.className=\'activeField seoy-large\'" value="" />
								<button id="post_code_btn" class="combobox">post_code</button>
							</div>
						</td>
					</tr>
				</table>
	        </fieldset>	
    </div>
    ';
	return $data;
}

?>